﻿//-----------------------------------------------------------------------
// <copyright file="CurrentSettings.cs" company="FIT VUT">
//     Copyright (c) FIT VUT. All rights are reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Device.Location;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BookCaching.Data;

namespace BookCaching.Settings
{
    public enum OrderingOrder
    {
        ascending,
        descending
    }

    public enum SortingPlacesBy
    {
        distance,
        name
    }

    public enum FilterByCountry
    {
        all
    }

    public sealed class CurrentSettings : IValueConverter, INotifyPropertyChanged
    {
        #region Private fields
        #region Singleton instance
        private static CurrentSettings instance = new CurrentSettings();
        #endregion

        #region General settings fields
        private SolidColorBrush backgroundColor;
        private SolidColorBrush foregroundColor;
        private User user;
        private GeoCoordinateWatcher currentPositionWatcher;
        private GeoCoordinate currentPosition;
        #endregion

        #region Book list settings fields
        private OrderingOrder bookListOrderingOrder;
        private Visibility bookListThumbnailsVisibility;
        #endregion

        #region Place list settings fields
        private OrderingOrder placeListOrderingOrder;
        private SortingPlacesBy placeListSortingBy;
        private FilterByCountry placeListFilerByCountry;
        private Visibility placeIconVisibility;
        private double distance;
        #endregion

        #region Data dictionaries
        private Dictionary<int, Place> places;
        #endregion Data dictionaries
        #endregion

        #region Constructors
        private CurrentSettings()
        {

        }
        #endregion

        #region Events
        public event PropertyChangedEventHandler PropertyChanged;
        public event EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>> CurrentPositionChanged;
        #endregion

        #region Public properties
        #region Singleton accessor
        /// <summary>
        /// Gets the singleton's instance.
        /// </summary>
        public static CurrentSettings Instance
        {
            get
            {
                return instance;
            }
        }
        #endregion

        #region General settings properties
        /// <summary>
        /// Gets or sets the current background color of the application.
        /// </summary>
        public SolidColorBrush BackgroundColor
        {
            get
            {
                return this.backgroundColor;
            }

            set
            {
                this.backgroundColor = value;
                this.OnPropertyChanged("BackgroundColor");
            }
        }

        /// <summary>
        /// Gets or sets the current foreground color of the application.
        /// </summary>
        public SolidColorBrush ForegroundColor
        {
            get
            {
                return this.foregroundColor;
            }

            set
            {
                this.foregroundColor = value;
                this.OnPropertyChanged("ForegroundColor");
            }
        }

        /// <summary>
        /// Gets or sets the currently logged user.
        /// </summary>
        public User User
        {
            get
            {
                return this.user;
            }

            set
            {
                this.user = value;
            }
        }

        public GeoCoordinate CurrentPosition
        {
            get
            {
                return currentPosition;
            }
        }
        #endregion

        #region Book list settings properties
        /// <summary>
        /// Gets or sets the current ordering for the list of books.
        /// </summary>
        public OrderingOrder BookListOrderingOrder
        {
            get
            {
                return this.bookListOrderingOrder;
            }

            set
            {
                this.bookListOrderingOrder = value;
            }
        }

        /// <summary>
        /// Gets or sets the visibility setting for the list of books.
        /// </summary>
        public Visibility BookListThumbnailsVisibility
        {
            get
            {
                return this.bookListThumbnailsVisibility;
            }

            set
            {
                this.bookListThumbnailsVisibility = value;
            }
        }
        #endregion

        #region Place list settings properties
        public OrderingOrder PlaceListOrderingOrder
        {
            get
            {
                return placeListOrderingOrder;
            }
            set
            {
                placeListOrderingOrder = value;
            }
        }

        public SortingPlacesBy PlaceListSortingBy
        {
            get
            {
                return placeListSortingBy;
            }
            set
            {
                placeListSortingBy = value;
            }
        }

        public FilterByCountry PlaceListFilerByCountry
        {
            get
            {
                return placeListFilerByCountry;
            }
            set
            {
                placeListFilerByCountry = value;
            }
        }

        public Visibility PlaceIconVisibility
        {
            get
            {
                return this.placeIconVisibility;
            }

            set
            {
                this.placeIconVisibility = value;
            }
        }

        public double Distance
        {
            get
            {
                return distance;
            }
            set
            {
                distance = value;
            }
        }
        #endregion
        #endregion

        #region Initialization
        public void Initialize()
        {
            //instance.BackgroundColor = new SolidColorBrush((Color)Application.Current.Resources["PhoneBackgroundColor"]);
            instance.BackgroundColor = new SolidColorBrush(DefaultSettings.Instance.BackGroundColor);
            instance.ForegroundColor = new SolidColorBrush(DefaultSettings.Instance.ForeGroundColor);
            instance.BookListOrderingOrder = DefaultSettings.Instance.BookListOrderingOrder;
            instance.BookListThumbnailsVisibility = DefaultSettings.Instance.BookListThumbnailsVisibility;
            instance.PlaceListOrderingOrder = DefaultSettings.Instance.PlaceListOrderingOrder;
            instance.PlaceListSortingBy = DefaultSettings.Instance.PlaceListSortingBy;
            instance.PlaceListFilerByCountry = DefaultSettings.Instance.PlaceListFilerByCountry;
            instance.Distance = DefaultSettings.Instance.Distance;
            instance.User = DefaultSettings.Instance.User;
            this.currentPositionWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
            this.currentPositionWatcher.PositionChanged += currentLocationWatcher_PositionChanged;
            this.currentPositionWatcher.Start();
        }
        #endregion

        #region Other public methods
        public void SetTheme(Color foregroundColor, Color backgroundColor)
        {
            this.ForegroundColor = new SolidColorBrush(foregroundColor);
            this.BackgroundColor = new SolidColorBrush(backgroundColor);
        }
        #endregion Other public methods

        #region Data dictionaries handlers
        public void AddPlace(Place place)
        {
            this.places.Add(place.ID, place);

        }

        public void RemovePlace(Place place)
        {
        }
        #endregion Data dictionaries handlers

        #region IValueConverter implementation
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch ((string)parameter)
            {
                case "ForegroundColor":
                    return this.ForegroundColor.ToString();
                case "BackgroundColor":
                    return this.BackgroundColor.ToString();
                default:
                    return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // ConvertBack is not used, because the Localization is only a One Way binding
            throw new NotImplementedException();
        }
        #endregion

        #region EventHandlers
        private void OnPropertyChanged(string name)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        void currentLocationWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            this.currentPosition = e.Position.Location;            
            this.CurrentPosition.Latitude = 50.073;
            this.CurrentPosition.Longitude = 14.434;

            if (this.CurrentPositionChanged != null)
            {
                this.CurrentPositionChanged(this.currentPositionWatcher, e);
            }
        }
        #endregion
    }
}
